//
//  APPTipAlertViewController.swift
//  APP
//
//  Created by LXF on 2018/1/12.
//  Copyright © 2018年 Rdxer. All rights reserved.
//

import UIKit
import Spring


public class APPAlertViewControllerStyle{
    public var backgroundColor  = UIColor.init(hex: "#F7F7F7")
    public var textColor = UIColor.init(hex: "#333333")
    public var mainColor = UIColor.init(hex: "#66ACF0")
    public var lightTextColor = UIColor.init(hex: "#757575")
}

/// 两行
open class APPTipAlertViewController: UIViewController {
    
    public  var style:APPAlertViewControllerStyle = APPAlertViewControllerStyle.init()
    
    public  var margin:CGFloat = 8
    public  var rowHeight:CGFloat = 45
    public  var w:CGFloat = 254
    
    open var backView = SpringView.init().then {
        $0.backgroundColor = UIColor.init(white: 0, alpha: 0.5)
    }
    
    open var containerView = SpringView.init().then({
        $0.backgroundColor = UIColor.white
        $0.layer.cornerRadius = 3
        $0.clipsToBounds = true
    })
    
    open lazy var message_Label = UILabel.init().then({
        $0.textColor = style.textColor
//        $0.font = AUIC.h2Size.toFont
        $0.font = 15.toFont
        $0.textAlignment = .center
        $0.numberOfLines = 0
    })
    
    /// 底部摁钮
    open lazy var buttonsContainerView = UIView.init().then {
        $0.backgroundColor = UIColor.white
        $0.xx.add(divider: .top, dividerColor: style.backgroundColor, dividerWidth: 1, dividerEdgeInset: UIEdgeInsets.zero)
    }
    
    open lazy var cancelButton = UIButton.init(type: UIButton.ButtonType.system).then({
        $0.setTitle("取消".i18n, for: .normal)
        $0.setTitleColor(style.textColor, for: .normal)
        $0.xx.add(divider: .right, dividerColor: style.backgroundColor, dividerWidth: 1, dividerEdgeInset: UIEdgeInsets.zero)
    })
    
    open lazy var confirmButton = UIButton.init(type: UIButton.ButtonType.system).then({
        $0.setTitle("确定".i18n, for: .normal)
        $0.setTitleColor(style.mainColor, for: .normal)
    })
    
    /// callback
    
    open var dismissedCallBack:((_ alert:APPTipAlertViewController)->())?
    open var canConfirmCallBack:((_ alert:APPTipAlertViewController)->Bool)?
    open var clickConfirmButtonCallBack:((_ alert:APPTipAlertViewController)->())?
    open var clickCancelButtonCallBack:((_ alert:APPTipAlertViewController)->())?
    
    open func setClickConfirmButtonCallBack(callback: ((_ alert:APPTipAlertViewController)->())?){
        self.clickConfirmButtonCallBack = callback
    }
   open func setClickCancelButtonCallBack(callback: ((_ alert:APPTipAlertViewController)->())?){
        self.clickCancelButtonCallBack = callback
    }
 
    
    @discardableResult
    public init() {
        super.init(nibName: nil, bundle: nil)
    }
    
    /// 显示到
    @discardableResult
    open func show(at vc:UIViewController?)->APPTipAlertViewController{
        self.removeFromParent()
        self.view.removeFromSuperview()
        vc?.addChild(self)
        vc?.view.addSubview(self.view)
//        self.view.frame = (vc?.view.bounds) ?? .zero
        self.view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        containerView.animation = "zoomIn"
        containerView.animate()
        return self
    }
    
    /// 显示到 rootViewController
    @discardableResult
    open func show()->APPTipAlertViewController{
        return self.show(at: UIApplication.shared.keyWindow!.rootViewController)
    }
    
    /// 隐藏 消失
    open override func dismiss(animated flag: Bool = true, completion: (() -> Void)? = nil) {
        self.view.removeFromSuperview()
        self.removeFromParent()
        dismissedCallBack?(self)
        completion?()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override open func viewDidLoad() {
        super.viewDidLoad()
        prepare()
        prepareSubViews()
        layoutSubViews()
        prepareEvent()
    }
    
    open func prepare(){
        view.backgroundColor = UIColor.clear
        containerView.autostart = false
    }
    
    open func prepareSubViews(){
        view.addSubviews([
            backView,
            containerView.addSubviews([
                message_Label,
                buttonsContainerView.addSubviews([
                    cancelButton,
                    confirmButton
                    ])
                ])
            ])
    }
    
    open func layoutSubViews(){
        
        backView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        containerView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(w)
        }
        
        buttonsContainerView.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(rowHeight)
        }
        
        message_Label.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
                .offset(margin)
            make.bottom.equalTo(buttonsContainerView.snp.top)
                .offset(-margin)
            make.left.equalToSuperview().offset(margin)
            make.right.equalToSuperview().offset(-margin)
            make.height.greaterThanOrEqualTo(rowHeight * 1.5)
        }
        
        buttonsContainerView.xx_HorizontalTile(withSubviews: [cancelButton,confirmButton].filter({ btn in
            btn.isHidden == false
        }), insets: .zero)
    }
    
    open func prepareEvent(){
        cancelButton.addTarget(self, action: #selector(clickCancelButton), for: .touchUpInside)
        confirmButton.addTarget(self, action: #selector(clickconfirmButton), for: .touchUpInside)
    }
    
    @objc func clickCancelButton(){
        if let clickCancelButtonCallBack = clickCancelButtonCallBack {
            clickCancelButtonCallBack(self)
        }else{
            dismiss(animated: true, completion: nil)
        }
    }
    
    @objc func clickconfirmButton(){
        
        if let canConfirmCallBack = canConfirmCallBack,!canConfirmCallBack(self) {
            return;
        }
        
        if let clickConfirmButtonCallBack = clickConfirmButtonCallBack {
            clickConfirmButtonCallBack(self)
        }else{
            dismiss(animated: true, completion: nil)
        }
        
    }
    deinit {
        XXLoger.w("\(self) - 已释放")
    }

}
